The first service to service
communication type is direct, synchronous
request response. Let's start with some basics. If you want to send
a request from one Cloud Run
service to another, you'll need to know what
URL to send the request to. If you use the default URL, this URL is unique for every
combination of service, project, and region. Notably, Cloud Run does
not have an internal DNS, you need to send a request
to the external URL, which is unique
for every service. There are three options
for URL discovery. You can configure the URLs of other services using
environment variables. This is the preferred
and portable solution as you can use environment
variables anywhere, even if you start your container
on your local machine. Alternatively, you
can request a list of all Cloud Run services in a project as your
service starts. To get this list
called a Cloud Run Admin API when your
container starts, you'll save your
configuration overhead with this solution. At the expense of increased
run-time complexity, this solution is not portable
outside of Cloud Run. Finally, if you're looking
for a fast solution, you can use your knowledge of the format of Cloud
Run service URL to figure out the URLs of other services in the
same project and region. The recommended and most
portable solution is to pass service URLs
as a configuration. On Cloud Run, you can pass configuration with
environment variables. In this example,
I'm passing the URL of the product service with the environment
variable, PRODUCT_URL. In your application, access the environment variable
when you need to make a request to the
product back-end. One of the benefits of this
approach is that it lets you easily replace the URLs in
a different environment, for example, on
your own machine. You can also use environment
variables to pass the connection parameters
of other services, like a database
connection string or the address of
a third-party API. Be careful though, not to pass
secrets like for example, API keys and passwords as
environment variables. Doing so is a big security risk. Instead, to pass sensitive
configuration data, use the integration between
Cloud Run and Secret Manager. If you have many services, maintain the list of services manually will quickly
become mature. However, if you use
an infrastructure as code tool such as Terraform, it is easy to generate
this list automatically. An alternative
approach is to call the Cloud Run Admin
API from your service. There is an endpoint
in the Admin API that let you list all the
services in a project. It will give you a mapping from the service name to service URL. This is a convenient solution
with a few drawbacks. The run-time complexity
increases because the Cloud Run Admin API
is another dependency. Your service need to
call the Cloud Run API, but you don't want
to give your service full permissions to that API, you might accidentally
grant to broad permissions. The approach is not portable
outside of Cloud Run, and this method is no
longer easily extendable to other service endpoints like a database or a
third-party service. Finally, if you're looking
for a fast solution, you can use knowledge
of the structure of the Cloud Run default URL, or use a custom domain to infer the URLs of
other services. This is an example
of a public URL and the parts of that
which it is composed of. Firstly, you have
the service name, it's the name you
gave to your service. Then you have the
product ID hash, this is a globally
unique identifier used for all services
in a project. There is a small
chance a project has a different hash for a Cloud Run service in
a different region. Finally, there's
the region code. UC equals US Central 1, and EU equals Europe, West 1. All other regions have
similar abbreviations. The main drawbacks here are that this solution is not portable
outside of Cloud Run, and while unlikely, the URL structure might
change in the future. To summarize, if you
want to make requests from one Cloud Run
service to another, you need to know its
globally unique URL. Cloud Run services do not
have an internal URL, which means you need a way
to discover other URLs. You can use environment
variables to pass other service URLs as
configuration during deployment. Alternatively, you can call to the Cloud Run Admin
API to request a list of all services
in a project.